package com.study.samsung.Control.htControl;

import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Entity.Commodity;
import com.study.samsung.Service.CommodityService;
import com.study.samsung.Service.GoodsListService;
import com.study.samsung.Service.GoodsService;
import com.study.samsung.Utils.BaseServlet;
import com.study.samsung.Utils.Pager;
import com.study.samsung.Utils.Pagers;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/htgoodslist")
public class GoodsListServlet extends BaseServlet {
    CommodityService cs = new CommodityService();
    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        Object obj = session.getAttribute("admin");
        if (obj==null){
            return "f:htjsp/login";
        }else {
            String inputvalue = request.getParameter("inputvalue");
            Integer pageNo = 1,pageSize=5;
            Pagers pagers = cs.htselectAll(pageNo, pageSize, inputvalue);
            System.out.println(pagers);
            session.setAttribute("pagers",pagers);
            return "f:htjsp/goods-list";
        }

    }

    public JSONObject goodsFenye(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject jsonObject = new JSONObject();
        Integer pageNo;
        String inputvalue = request.getParameter("inputvalue");
        if (request.getParameter("pageNo")==null){
            pageNo=1;
        }else {
            pageNo=Integer.parseInt(request.getParameter("pageNo"));
        }
        Pagers pagers = cs.htselectAll(pageNo, 5, inputvalue);
        jsonObject.put("pagers",pagers);
        return jsonObject;
    }

    public Integer changeisdefauledown(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        Integer integer = cs.changeDown(i);
        return integer;
    }

    public Integer changeisdefauleup(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        Integer integer = cs.changeUp(i);
        return integer;
    }

    public Integer delgoods(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");

        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        Integer delgoods = cs.delgoods(i);
        return delgoods;
    }
}
